package com.ysd.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

/**
 * Servlet implementation class UploadServlet
 */
@WebServlet("/upload")
public class UploadServlet extends BaseServlet {
	//localhost:8080/guangneidian/upload?method=uploadImg
	public void uploadImg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		SmartUpload smart=new SmartUpload();//创建对象
		smart.setCharset("UTF-8");
		smart.initialize(getServletConfig(), request, response);//初始化
		try {
			smart.upload();
			smart.save("/images");
			String fileName=smart.getFiles().getFile(0).getFileName();
			out.print(Result.toClient("0", "上传成功", 0, fileName));
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			out.print(Result.toClient("1", "上传失败", 0, ""));
			e.printStackTrace();
		}
		
		out.flush();
		out.close();
	}
}
